<?php $db = new Mypdo(); ?><?php

if (!empty($_SESSION['login'])&&!empty($_SESSION['email'])){

if(!empty($_GET['supprid'])&&!empty($_GET['id'])){


    if($_GET['supprid'] == $_GET['id']){
        $propriManager = new ProprietaireManager($db);
        $persManager = new PersonneManager($db);
        $locataireManager = new LocataireManager($db);
        $photoManager = new PhotoManager($db);
        $chargeManager = new ChargeManager($db);
        $locationManager = new LocationManager($db);
        $immeubleManger = new ImmeubleManager($db);
        $messageManager = new MessageManager($db);
        $loyerManager = new LoyerManager($db);


        $loyerManager->delLoyerByIdPropri($_GET['supprid']);
        $persManager->delPersByIdPropri($_GET['supprid']);
        $locataireManager->delLocaByIdPropri($_GET['supprid']);
        $chargeManager->delChargeByIdPropri($_GET['supprid']);
        $photoManager->delPhotoByIdPropri($_GET['supprid']);
        $locationManager->delLocationByIdPropri($_GET['supprid']);
        $immeubleManger->delImmeubleByIdPropri($_GET['supprid']);
        $messageManager->delMsgByIdPropri($_GET['supprid']);
        $propriManager->delPropri($_GET['supprid']);
        header('Location: index.php?page=105');
    }
}else{
    if(empty($_POST)&&!empty($_GET['id'])){
        $propriManager = new ProprietaireManager($db);
        $propri = $propriManager->getPropri($_GET['id']);
        ?>

        <h1>Détail proprietaire</h1>

        <form method="post" action="#">

            <p>
            <h3>Identité</h3>
            <label for="genre_propri">Genre :</label><select name="genre_propri" id="genre_propri"  required="required">
                <option></option>
                <option value="M" <?php echo $propri->getGenrePropri() == 'M'?'selected':''; ?>>M.</option>
                <option value="Mme" <?php echo $propri->getGenrePropri() == 'Mme'?'selected':''; ?>>Mme.</option>
                <option value="Mlle" <?php echo $propri->getGenrePropri() == 'Mlle'?'selected':''; ?>>Mlle.</option>
            </select>
            <label for="nom_propri">Nom:</label><input name="nom_propri" type="text" id="nom_propri" required="required" value="<?php echo $propri->getNomPropri(); ?>" />
            <label for="prenom_propri">Prenom :</label><input type="text" name="prenom_propri" id="prenom_propri" required="required" value="<?php echo $propri->getPrenomPropri(); ?>" /><br />
            <label for="siret_propri">Siret :</label><input type="text" name="siret_propri" id="siret_propri" value="<?php echo $propri->getSiretPropri(); ?>" />
            <label for="ape_propri">Ape :</label><input type="text" name="ape_propri" id="ape_propri" value="<?php echo $propri->getApePropri(); ?>" /><br />
            </p>
            <p>
            <h3>Adresse</h3>

<?php  $adresse = $propri->getAdressePropri();  ?>

            <p>
                <label for="num_adresse">N°:</label><input name="num_adresse" type="text" id="num_adresse" min="0" value="<?php echo $adresse->getNumAdresse(); ?>" />
                <label for="voie_adresse">Voie :</label><input type="text" name="voie_adresse" id="voie_adresse" value="<?php echo $adresse->getVoieAdresse(); ?>" /><br />
                <label for="cp_adresse">Code Postal :</label><input type="text" name="cp_adresse" id="cp_adresse" value="<?php echo $adresse->getCpAdresse(); ?>" />
                <label for="ville_adresse">Ville :</label><input type="text" name="ville_adresse" id="ville_adresse" value="<?php echo $adresse->getVilleAdresse(); ?>"/><br />
            </p>



            <p>
            <h3>Contact</h3>
            <label for="num_fix_propri">Numero fix</label><input name="num_fix_propri" type="text" id="num_fix_propri" required="required" value="<?php echo $propri->getNumFixPropri(); ?>" />
            <label for="num_port_propri">Numero portable :</label><input type="text" name="num_port_propri" id="num_port_propri" value="<?php echo $propri->getNumPortPropri(); ?>" /><br />
            <label for="email_propri">Email :</label><input type="text" name="email_propri" id="email_propri" value="<?php echo $propri->getEmailPropri(); ?>" />
            </p>


            <p><input type="reset" value="Annuler" /><input type="submit" value="Modifier" />
                <input id="suppr" name="suppr" type="button" onclick="document.location.href='index.php?page=111&id=<?php echo $_GET['id']; ?>&supprid=<?php echo $propri->getIdPropri(); ?>'" value="Supprimer" />
            </p>
        </form>
        <?php
        $immeuManager = new ImmeubleManager($db);
        $immeu = $immeuManager->getListByIdPropri($_GET['id']);
        ?>
        <h1>Liste des habitations</h1>
        <table class="tableau" >
            <tr>
                <th>Nom</th>
                <th>Détail</th>
            </tr>
            <?php foreach ($immeu as $valeur) {  ?>

                <tr>
                    <td><?php echo $valeur->getNomImmeu(); ?></td>
                    <td><input id="detail" name="detail" type="button" onclick="document.location.href='index.php?page=112&id=<?php echo $valeur->getIdImmeu(); ?>'" /></td>
                </tr>

            <?php } ?>
        </table>
        <input value="Ajouté une habitation" id="add" name="add" type="button" onclick="document.location.href='index.php?page=102&idp=<?php echo $_GET['id']; ?>'" />


    <?php }else{
        $propriManager = new ProprietaireManager($db);
        $propriN = new Proprietaire($_POST);
        $propriN->setIdPropri($_GET['id']);
        $propriN->setAdressePropri(new Adresse($_POST));
        $propri = $propriManager->getPropri($_GET['id']);




        $propriManager->update($propriN);
        header('Location: #');
    }
}
}else{
    header('Location: index.php');
}